package com.qf.gh.dao.impl;

import com.qf.gh.dao.GoodsDao;
import com.qf.gh.domain.Goods;
import com.qf.gh.domain.User;
import com.qf.gh.utils.DateSourceUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

public class GoodsDaoImpl implements GoodsDao {
    @Override
    public long findTotalCount(int typeId) {
        QueryRunner qr=new QueryRunner(DateSourceUtils.getDataSource());
        String sql="select count(0) from tb_goods where typeid = ?";
        try {
            Long totalCount=(Long) qr.query(sql,new ScalarHandler(),typeId);
            return totalCount;
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("获取条数失败");
        }
        return 0;
    }

    @Override
    public List<Goods> findData(int typeId, int i, int pageSize) {
        QueryRunner qr = new QueryRunner(DateSourceUtils.getDataSource());
        String sql = "select id,name,pubdate,picture,price,star,intro,typeid from tb_goods where typeid = ? limit ?, ?";
        try {
            List<Goods> list = qr.query(sql,new BeanListHandler<>(Goods.class),typeId,i,pageSize);
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("获取每页数据失败");
        }
        return null;
    }

    @Override
    public Goods selectById(int id) {
        QueryRunner qr=new QueryRunner(DateSourceUtils.getDataSource());
        String sql="select id,name,pubdate,picture,price,star,intro,typeid from tb_goods where id = ?";
        try {
            Goods goods=(Goods) qr.query(sql,new BeanHandler<>(Goods.class),id);
            return goods;
        } catch (SQLException e) {
            e.printStackTrace();
        }
      return null;
    }

    @Override
    public List<Goods> select() {
        QueryRunner qr = new QueryRunner(DateSourceUtils.getDataSource());
        String sql = "select id,name,pubdate,picture,price,star,intro,typeid from tb_goods ";
        try {
            List<Goods> list = qr.query(sql,new BeanListHandler<>(Goods.class));
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("获取商品列表失败");
        }
        return null;
    }

    @Override
    public void add(Goods goods) {
        QueryRunner qr = new QueryRunner(DateSourceUtils.getDataSource());
        String sql = "insert into tb_goods (id,name,pubdate,picture,price,star,intro,typeid) values (null,?,?,?,?,?,?,?) ";
        try {
         qr.update(sql,goods.getName(),goods.getPubdate(),goods.getPicture(),goods.getPrice(),goods.getStar(),goods.getIntro(),goods.getTypeid());

        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("增加商品失败");
        }
    }

    @Override
    public void delete(int parseInt) {
        String sql="delete from tb_goods where id=?";
        QueryRunner qr=new QueryRunner(DateSourceUtils.getDataSource());
        try {
            qr.update(sql,parseInt);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("删除商品失败");
        }
    }

    @Override
    public void update(Goods goods, int id) {
        String sql="update tb_goods set name=?,pubdate=?,picture=?, price=? ,star=? ,intro=?, typeid=? where id=?";
        QueryRunner qr=new QueryRunner(DateSourceUtils.getDataSource());
        try {
            qr.update(sql,goods.getName(),goods.getPubdate(),goods.getPicture(),goods.getPrice(),goods.getStar(),goods.getIntro(),goods.getTypeid(),id);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("修改商品失败");
        }
    }

    @Override
    public List<Goods> find(String toString, List<Object> parmas) {
        QueryRunner qr=new QueryRunner(DateSourceUtils.getDataSource());
        String sql="select id,name,pubdate,picture,price,star,intro,typeid from tb_goods"+toString;
        try {
            List<Goods> goods=qr.query(sql,new BeanListHandler<>(Goods.class),parmas.toArray());
            return goods;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
